package org.niux.AutoLoginQQ.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.niux.AutoLoginQQ.common.Constant;

public class ModifyPasswordDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private String qqNumString;
	private Text newPassword;
	private Main gui;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public ModifyPasswordDialog(Shell parent, int style,String qqNumString,Main gui) {
		super(parent, style);
		setText("SWT Dialog");
		this.qqNumString = qqNumString;
		this.gui = gui;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		
		//窗口居中
		int width = shell.getMonitor().getClientArea().width;  
		int height = shell.getMonitor().getClientArea().height;  
		
		int x = shell.getSize().x;  
		int y = shell.getSize().y; 
		
		if(x > width){  
			shell.getSize().x = width;  
		}  
		        
		if(y > height){  
			shell.getSize().y = height;  
		}  
		        
		shell.setLocation((width - x) / 2, (height - y) / 2);
		
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		
		//退出
//		shell.addShellListener(new ShellAdapter() {
//			public void shellClosed(ShellEvent e) {
//				Constant.VERIFYCODE = verifyCode.getText();
//			}
//		});
		
//		shell.addDisposeListener(new DisposeListener() {
//			public void widgetDisposed(DisposeEvent arg0) {
//				Constant.VERIFYCODE = verifyCode.getText();
//			}
//		});
			
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.SYSTEM_MODAL);
		
		shell.setBackground(SWTResourceManager.getColor(173, 216, 230));
		shell.setSize(212, 156);
		shell.setText("登录密码修改");
		
		Label lblQq = new Label(shell, SWT.NONE);
		lblQq.setBackground(SWTResourceManager.getColor(173, 216, 230));
		lblQq.setBounds(10, 10, 54, 18);
		lblQq.setText("QQ号码：");
		
		Label qqNum = new Label(shell, SWT.NONE);
		qqNum.setBackground(SWTResourceManager.getColor(173, 216, 230));
		qqNum.setBounds(70, 10, 126, 18);
		qqNum.setText(qqNumString);
		
		Button okBtn = new Button(shell, SWT.NONE);
		
		shell.setDefaultButton(okBtn);
		
		okBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(newPassword.getText() == null || "".equals(newPassword.getText())){
					gui.alert("新密码不能为空");
					return;
				}
				Constant.QQ_LIST.get(qqNumString).setPassword(newPassword.getText());
				Display.getDefault().asyncExec(new Runnable() {
					public void run() {
						gui.updatePassword(qqNumString);
					}
				});
				shell.close();
			}
		});
		okBtn.setBounds(10, 94, 72, 22);
		okBtn.setText("确定");
		
		Button cancleBtn = new Button(shell, SWT.NONE);
		cancleBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}
		});
		cancleBtn.setBounds(124, 94, 72, 22);
		cancleBtn.setText("取消");
		
		Label label = new Label(shell, SWT.NONE);
		label.setBackground(SWTResourceManager.getColor(173, 216, 230));
		label.setBounds(10, 34, 54, 17);
		label.setText("原密码：");
		
		Label oldPassword = new Label(shell, SWT.NONE);
		oldPassword.setBackground(SWTResourceManager.getColor(173, 216, 230));
		oldPassword.setBounds(70, 34, 126, 17);
		
		oldPassword.setText(Constant.QQ_LIST.get(qqNumString).getPassword());
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setBackground(SWTResourceManager.getColor(173, 216, 230));
		label_1.setBounds(10, 61, 54, 17);
		label_1.setText("新密码：");
		
		newPassword = new Text(shell, SWT.BORDER);
		newPassword.setBounds(70, 57, 126, 23);
		newPassword.setFocus();

	}

}
